<template>
  <Panel>
    <template
      >订单管理
      <!-- 1查询的表单 -->
      <el-form :inline="true" :model="searchobj" class="demo-form-inline">
        <el-form-item label="订单号" size="mini">
          <el-input v-model="searchobj.orderNo" placeholder="订单号"></el-input>
        </el-form-item>
        <el-form-item label="收货人" size="mini">
          <el-input
            v-model="searchobj.consignee"
            placeholder="收货人"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号" size="mini">
          <el-input v-model="searchobj.phone" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item label="订单状态" size="mini">
          <el-select v-model="searchobj.orderState" placeholder="订单状态">
            <el-option label="已受理" value="已受理"></el-option>
            <el-option label="派送中" value="派送中"></el-option>
            <el-option label="已完成" value="已完成"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择时间" size="mini">
          <el-date-picker
            v-model="searchobj.date"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getorderList('s')">查询</el-button>
        </el-form-item>
      </el-form>
      <!-- 2渲染数据的表格 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column fixed prop="orderNo" label="订单号" width="150">
        </el-table-column>
        <el-table-column label="下单时间" width="120">
          <template v-slot="{ row }">
            {{ row.orderTime | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号" width="120">
        </el-table-column>
        <el-table-column prop="consignee" label="收货人" width="120">
        </el-table-column>
        <el-table-column prop="deliverAddress" label="配送地址" width="120">
        </el-table-column>
        <el-table-column label="送达时间" width="120">
          <template v-slot="{ row }">
            {{ row.deliveryTime | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column prop="remarks" label="用户备注" width="120">
        </el-table-column>
        <el-table-column prop="orderAmount" label="订单金额" width="120">
        </el-table-column>
        <el-table-column prop="orderState" label="订单状态" width="120">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template v-slot="{ row }">
            <el-button @click="handleClick(row)" type="text" size="small"
              >查看</el-button
            >
            <!--  @click="$router.push('/dingdan/edit?id=' + row.id)"路由传参 1 -->
            <!--  @click="
                $router.push({ path: '/dingdan/edit', query: { id: row.id } })
              " 路由传参 2-->
            <!-- @click="$router.push(`/dingdan/edit/${row.id}`)" 路由传参 3 -->
            <!-- @click="
                $router.push({ name: 'orderEdit', params: { id: row.id } })
              " 路由传参 4  -->
            <el-button
              type="text"
              size="small"
              @click="
                $router.push({ name: 'orderEdit', params: { id: row.id } })
              "
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 3分页 -->
      <div class="block">
        <span class="demonstration">完整功能</span>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[2, 5, 10, 15]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
      <!-- 4查看订单详情 -->
      <el-dialog title="提示" :visible.sync="dialogVisible" width="50%">
        <div>{{ curDate }}</div>
        <!-- 时间线 -->
        <el-timeline :reverse="reverse">
          <el-timeline-item
            v-for="(activity, index) in activities"
            :key="index"
            :timestamp="activity.timestamp"
          >
            {{ activity.content }}
          </el-timeline-item>
        </el-timeline>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false" size="medium"
            >取 消</el-button
          >
          <el-button type="primary" @click="dialogVisible = false" size="medium"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </template>
  </Panel>
</template>

<script>
import { orderList, orderinquire } from '../../api/orders.js'
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      /* 显示查看订单详情框的属性false就是消失 */
      dialogVisible: false,
      curDate: {},
      // 过多的参数可以放在对象里面管理
      searchobj: {
        orderNo: '',
        consignee: '',
        phone: '',
        orderState: '',
        date: []
      },
      formInline: {
        user: '',
        region: ''
      },
      // 下面都是时间线的方法
      reverse: true,
      activities: [
        {
          content: '已受理',
          timestamp: '2018-04-15'
        },
        {
          content: '派送中',
          timestamp: '2018-04-13'
        },
        {
          content: '已完成',
          timestamp: '2018-04-11'
        }
      ]
    }
  },
  created() {
    this.getorderList()
  },
  methods: {
    /* 获取订单管理请求 */
    async getorderList(s) {
      // 时间的data需要是字符串的数组
      const searcdata = JSON.parse(JSON.stringify(this.searchobj))
      searcdata.data = JSON.stringify(searcdata.data)
      // 封装一个obj对象,方便共用
      const obj = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        ...searcdata
      }
      let res = null
      if (s === 's') {
        res = await orderList(obj)
      } /* 查询 */ else {
        // 正常请求
        res = await orderinquire(obj)
      }
      /* const { data, total } = await orderList({obj}) */
      // console.log(data)
      this.tableData = res.data
      this.total = res.total
    },
    /* handleClick拿到当前行的数据并显示 */
    handleClick(row) {
      this.dialogVisible = true
      console.log(row)
      // 将这行的数据给curDate
      this.curDate = row
    },
    handleSizeChange(val) {
      //  console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getorderList()
    },
    handleCurrentChange(val) {
      //  console.log(`当前页: ${val}`)
      this.currentPage = val
      this.getorderList()
    }
    // 点击这个按钮发送查询请求
    /* onSubmit() { */
    //  console.log('submit!')
    /*  const data=await orderinquire({

    })
    } */
  }
}
</script>

<style lang="less" scoped></style>
